import re
from lib import sbr_betaseries

class EpisodeParser:
    FileRegexp = re.compile( r"""
        (?P<show>.*?)\.                     # Show title
        # BEGIN season/episode matching
        [sS]?(?P<season>[0-9]{1,2})[eE]?(?P<episode>[0-9]{2}) # sXXeYY, XXeYY, SXXEYY, XXYY
            (?:-[eE](?P<episode2>[0-9]{2}))? #-eAA (double eps)
        # END season/episode matching
        \.(?P<misc>[^\-]*)  # misc (title, def, etc)
        -([a-zA-Z0-9]+)     # group
        \.(mkv|avi)         # extension
        """, re.VERBOSE )
    
    valid = 1
    ShowName = ''
    Season = Episode = Episode2 = 0
    DoubleEpisode = 0
    
    def __init__( self, filename ):
        print "Matching on " + filename
        matches = self.FileRegexp.match( filename )
        if not matches:
            self.valid = 0
            return
        self.ShowName = matches.group( 'show' )
        self.Season   = matches.group( 'season')
        self.Episode  = matches.group( 'episode')
        if ( matches.group( 'episode2' ) ):
            self.DoubleEpisode = 1
            self.Episode2 = matches.group( 'episode2' )
        else:
            self.DoubleEpisode = 0
            
    def __str__(self):
        return str( {"show":     self.ShowName,
                     "season":   self.Season,
                     "episode":  self.Episode,
                     "episode2": self.Episode2,
                     "double":   self.DoubleEpisode} )
        

